package com.xywl.escort.view.operation

import android.graphics.Paint
import android.os.Bundle
import android.support.v4.content.ContextCompat
import android.support.v4.widget.SwipeRefreshLayout
import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.trello.rxlifecycle2.LifecycleTransformer
import com.xywl.escort.R
import com.xywl.escort.adapter.OperationRecordAdapter
import com.xywl.escort.base.BaseActivity
import com.xywl.escort.bean.OperationRecordResp
import com.xywl.escort.widget.HorizontalDividerItemDecoration
import dagger.android.AndroidInjection
import kotlinx.android.synthetic.main.activity_operation_record.*
import java.util.*
import javax.inject.Inject

@Route(path = "/xy/operation/record")
class OperationRecordActivity : BaseActivity(), OperationRecordContract.View, SwipeRefreshLayout.OnRefreshListener {

    private lateinit var mAdapter: OperationRecordAdapter
    private val mDataBeanList = ArrayList<OperationRecordResp.DataBean.ListBean>()
    @Inject
    lateinit var mPresenter: OperationRecordContract.Presenter

    @Autowired
    @JvmField
    var projectId = ""

    private var pageNum = 1
    private var isEnd: Boolean = false

    override fun onCreate(savedInstanceState: Bundle?) {
        AndroidInjection.inject(this)
        super.onCreate(savedInstanceState)
        ARouter.getInstance().inject(this)
        setContentView(R.layout.activity_operation_record)

        showBackButton()
        setTitleText(R.string.order_operation_record)
        initView()
        initData()
    }

    private fun initView() {
        mAdapter = OperationRecordAdapter(R.layout.item_operation_record, mDataBeanList)
        val paint = Paint()
        paint.strokeWidth = 2f
        paint.color = ContextCompat.getColor(this, R.color.line_color)
        paint.isAntiAlias = true
        recycler_operation_record.addItemDecoration(HorizontalDividerItemDecoration.Builder(this).paint(paint).build())
        recycler_operation_record.layoutManager = LinearLayoutManager(this)
        recycler_operation_record.adapter = mAdapter

        val emptyView = LayoutInflater.from(this).inflate(R.layout.layout_empty_view, null)
        val tvEmpty = emptyView.findViewById<TextView>(R.id.tv_empty)
        tvEmpty.text = "暂无记录"
        mAdapter.emptyView = emptyView

        swipeLayout.setOnRefreshListener(this)
        swipeLayout.setColorSchemeResources(R.color.colorAccent, R.color.colorPrimary, R.color.aid_prompt_color, R.color.colorBackground)
        mAdapter.setOnLoadMoreListener({
            swipeLayout.isEnabled = false
            recycler_operation_record.postDelayed({
                swipeLayout.isEnabled = false
                if (isEnd) {
                    //数据全部加载完毕
                    mAdapter.loadMoreEnd()
                    swipeLayout.isEnabled = true
                } else {
                    //成功获取更多数据
                    mPresenter.loadRecord(projectId, pageNum)
                }
            }, 200)
        }, recycler_operation_record)
    }

    private fun initData() {
        mPresenter.takeView(this)
        pageNum = 1
        mPresenter.loadRecord(projectId, pageNum)
    }

    override fun isActive(): Boolean {
        return false
    }

    override fun <T> bindToLife(): LifecycleTransformer<T> {
        return this.bindToLifecycle()
    }

    override fun onRefresh() {
        mAdapter.setEnableLoadMore(false)
        pageNum = 1
        mPresenter.loadRecord(projectId, pageNum)
    }

    override fun updateRecord(dataBean: OperationRecordResp.DataBean?) {
        swipeLayout.isRefreshing = false
        progress_bar.visibility = View.GONE
        recycler_operation_record.visibility = View.VISIBLE
        if (pageNum == 1) {
            mDataBeanList.clear()
        }
        if (pageNum >= dataBean!!.pages) {
            isEnd = true
        } else {
            isEnd = false
            pageNum += 1
        }
        mDataBeanList.addAll(dataBean.list)
        mAdapter.setNewData(mDataBeanList)
        mAdapter.loadMoreComplete()

    }

    override fun loadRecordFailure(msg: String?) {
        swipeLayout.isRefreshing = false
        progress_bar.visibility = View.GONE
        recycler_operation_record.visibility = View.VISIBLE
    }
}
